home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / recio212.zip / usage.txt < prev    next >
Text File  |  1995-01-29  |  32KB  |  886 lines

  1.     Title: STANDARD USAGE OF C LANGUAGE RECIO LIBRARY
  2. Copyright: (C) 1994-1995, William Pierpoint
  3.   Version: 2.12
  4.      Date: January 29, 1995
  5.  
  6.  
  7.  
  8. 1.0 INTRODUCTION
  9.  
  10. The implementation descibed by this standard usage is a superset of the
  11. recio specification.  Enhancements are noted in the text.
  12.  
  13.  
  14. 1.1 Mnemonics
  15.  
  16. The recio functions have been given a consistent mnemonic naming
  17. convention.  All recio functions are in lower case and start with
  18. the letter r.  Function names are analogous to <stdio.h> functions.
  19. Mnemonics are as follows:
  20.  
  21. Single letter (field functions)               Multi-letter
  22. ----------------------------------------      -----------------
  23. b - base (prefix)                             beg - beginning
  24. c - column (prefix), character (suffix)       ch  - character
  25. d - double (suffix)                           col - column
  26. f - float (suffix)                            cxt - context
  27. i - integer (suffix)                          eof - end of file
  28. l - long (suffix)                             err - error
  29. n - number                                    fld - field buffer
  30. r - record pointer (first letter)             fmt - format  
  31. s - string pointer (suffix)                   fn  - function
  32. t - time_t time (suffix)                      no  - number         
  33. u - unsigned (suffix)                         num - number
  34.                                                 rec - record buffer  
  35.                           siz - size of buffer
  36.                           str - string
  37.                           tm  - struct tm time
  38.                           txt - text           
  39.  
  40. 1.2 Order
  41.  
  42. The order in which the prefix mnemonics appear indicates the order in which
  43. the arguments appear in the function.  The suffix mnemonics of the input
  44. functions tell you what the function returns.  The suffix mnemonics of the
  45. output functions indicate the last argument and tell you what the function
  46. outputs.  All output functions return an integer with a zero value if the
  47. function executed successfully.
  48.  
  49. For example, the input function rbgetui():
  50.  
  51.     arguments:  r - record pointer
  52.                 b - base (radix) of input
  53.       returns: ui - unsigned integer
  54.  
  55. The output function rbputui():
  56.  
  57.     arguments:  r - record pointer
  58.                 b - base (radix) of input
  59.                ui - unsigned integer is output
  60.  
  61. Note: c is used in the prefix of a function's name only once even if there are
  62.       two column arguments.  If the function inputs or outputs a character,
  63.       there is only one column argument; otherwise there are two.
  64.  
  65.  
  66.  
  67. 2.0 ERRORS AND WARNINGS
  68.  
  69. The functions declared in the header <recio.h> make use of the errno macro
  70. defined in section 4.1.3 of ANSI X3.159-1989.  This mechanism was chosen
  71. because (1) the <stdlib.h> conversion functions (strtod(), strtol(), etc.)
  72. make use of this error reporting mechanism and (2) the <recio.h> functions
  73. make use of the <stdlib.h> conversion functions.
  74.  
  75. In this implementation, errno can return the following macro constants:
  76.  
  77.          0 - No error.
  78.     EACCES - permission denied.
  79.     EINVAL - invalid argument.
  80.     EMFILE - too many open files.
  81.     ENOENT - no such file or directory.
  82.     ENOMEM - out of memory.
  83.     ERANGE - data out of range.
  84.  
  85. where
  86.  
  87. * EACCESS means that you don't have permission to access this file.  All
  88.   MSDOS files have read permission.
  89.  
  90. * EINVAL indicates an invalid argument to a function, usually a NULL record
  91.   pointer.  This is most often caused by a programming error.
  92.  
  93. * EMFILE means the program tried to open more files than the maximum allotted
  94.   by ROPEN_MAX or FOPEN_MAX.  If your program is interactive, the user can
  95.   close one or more open record streams.  Or you might decide that ROPEN_MAX
  96.   or FOPEN_MAX needs to be a larger value.
  97.  
  98. * ENOENT says that ropen() could not find the requested file to open.
  99.   Perhaps the name of the file was misspelled, or your program looked in
  100.   wrong directory.  If your program was trying to read a configuration file,
  101.   it could use internal default values when the configuration file does
  102.   not exist.
  103.  
  104. * ENOMEM indicates that the program ran out of heap space.  You may be able
  105.   to correct this if you are able to deallocate memory you no longer need.
  106.   For example, you could reduce the size of buffers when the size only
  107.   affects speed.  Such buffers need to be flushed first.  Buffers used by
  108.   the recio library do not fit this criteria.
  109.  
  110. * ERANGE means that the data is outside the valid range.  The data value 
  111.   is either too large or too small.  Either the data is incorrect or you 
  112.   need a more robust data type to hold the data.
  113.  
  114. Beginning with version 1.1, recio functions set errno when the record
  115. pointer is invalid and set an internal error number when the record pointer
  116. is valid.  The recio error number is accessed through the rerror function.
  117.  
  118. The rerror function can return the following macro constants:
  119.  
  120.          0 - No error.
  121.   R_EINVAL - invalid argument (not the record pointer).
  122.  R_EINVDAT - invalid data.
  123.  R_EINVMOD - invalid mode.
  124.  R_EMISDAT - missing data.
  125.   R_ENOMEM - out of memory.
  126.   R_ENOPUT - unable to write data to output.
  127.   R_ERANGE - data out of range.
  128.   R_EFAULT - application defined error.
  129.  
  130. where
  131.  
  132. * R_EINVDAT says the data is invalid.  Invalid data is caused by an
  133.   unrecognized character in the field.  For example, rgetui() doesn't
  134.   expect to see a negative sign, so a negative number will be flagged as
  135.   invalid data.
  136.  
  137. * R_EINVMOD indicates that you opened a file in read mode, then used an output
  138.   function or opened a file in write or append mode, then used an input
  139.   function.
  140.  
  141. * R_EMISDAT says the data is missing.  Missing data means the field is empty.
  142.   If you expect a number, you could substitute either zero or some unique
  143.   number to indicate an empty field.
  144.  
  145. * R_ENOMEM indicates that the program ran out of heap space.  You may be able
  146.   to correct this if you are able to deallocate memory you no longer need.
  147.   For example, you could reduce the size of buffers when the size only
  148.   affects speed.  Such buffers need to be flushed first.  Buffers used by
  149.   the recio library do not fit this criteria.
  150.  
  151. * R_ENOPUT says the program was unable to write the data to output.  This
  152.   could indicate that the disk is full.
  153.  
  154. * R_ERANGE tells you that the data is outside the range of the function.
  155.   For instance, suppose you used rgeti() to get an integer and the data
  156.   value is 32768.  If a 16-bit integer has an upper limit of 32767, the
  157.   value is too large.  If the data is wrong, you can have the error
  158.   function correct it.  If the data is right, you have to correct the
  159.   data type in the program.
  160.  
  161. * R_EFAULT can be used with rseterr() in your own applications.  The 
  162.   recio library never issues a R_EFAULT error.
  163.  
  164. The recio library also has warning numbers.  There is not a global warning 
  165. number equivalent to errno.  The warning indicator is associated with a 
  166. record stream.  Thus warnings can only be set for valid record streams.  
  167. The warning number for a record stream is accessed through the rwarning 
  168. function.
  169.  
  170. The rwarning function can return the following macro constants:
  171.  
  172.          0 - No warning.
  173.  R_WEMPSTR - empty data string.
  174.   R_WNOREG - unable to register exit function with atexit().
  175.   R_WWIDTH - data too wide for columnar output.
  176.   R_WTMFMT - incomplete data in a time field.
  177.   R_WFAULT - application defined warning.
  178.  
  179. where
  180.  
  181. * R_WEMPSTR says that an empty data string was input.  If you want to
  182.   substitute another string from within your program, use the rsetfldstr 
  183.   function in your callback warning function to place the new string 
  184.   into the field buffer.
  185.  
  186. * R_WWIDTH indicates that on output, the data will not fit between the
  187.   columns specified.  If the data is numeric, the recio output functions
  188.   write asterisks to the output; if the data is a string, a truncated string
  189.   is written.
  190.  
  191. * R_WNOREG means the program was unable to register the internal recio exit
  192.   function with the ANSI atexit() function.  The internal recio exit
  193.   function ensures that all open record streams are closed and all dynamic
  194.   memory allocated by the recio library is deallocated.
  195.  
  196. * R_WTMFMT means the data in a time field was incomplete.  For example, 
  197.   the time format may have been looking for a date and a time of day but 
  198.   the time field only contained a date.  Data is only compared left to 
  199.   right.  Any mismatch in the delimiters that separate the time elements 
  200.   (month, hour, etc) generates an R_EINVDAT error.
  201.  
  202. * R_WFAULT can be used with rsetwarn() in your own applications.  The 
  203.   recio library never issues a R_WFAULT warning.
  204.  
  205.  
  206. 2.1 Define Callback Error Function
  207.  
  208. First define a callback error function to be used by the recio functions.
  209. You may give the function any name you wish.  In the sample function below,
  210. the name rerrfn is used.  The function takes one argument, a record pointer
  211. (REC *).  It returns nothing (void).  The function must first check for a
  212. valid record pointer using the risvalid function.  Other than that, you can
  213. customize it to do whatever you want.
  214.  
  215. A simple callback error function rerrmsg is included in the RECIO library.  
  216. In the initial prototyping stages of development, you may wish to use this 
  217. function rather than taking the time to develop your own function.  Later 
  218. you can substitute a more robust callback error function.
  219.  
  220. The recio functions use a callback error function in order to give the most 
  221. flexibility in handling errors.  The rerrfn function shown below just sends
  222. information to stderr.  You may wish to send information to a printer, a 
  223. file, a window, or a dialog box.  You might even want to give users the 
  224. ability to examine errors and enter corrections.  If the error is corrected, 
  225. you will want to call the rclearerr function before your callback error 
  226. function returns.
  227.  
  228. When your callback error function is invoked, check rerror() or errno
  229. to determine the cause of the error.
  230.  
  231. The main purpose of this sample callback error function is to show some of
  232. kinds of things you can do in a callback error function.  Note that when an
  233. error occurs, the column number indicator rcolno() has moved just beyond
  234. the error.  To make it clearer to the user where the error occurred, rerrfn()
  235. displays rcolno()-1, but not less than rbegcolno(), the column number for the
  236. first column.
  237.  
  238. A more detailed callback error function is given in the source code for the
  239. test program TESTCHG.C.  The test program callback error function makes use
  240. of the rfix functions to fix up bad data (primarily overflows and underflows)
  241. and continue processing.  If appropriate for your application, you can use
  242. these functions as well.  They have been compiled into the recio libraries
  243. for your potential use.
  244.  
  245. /* define callback error function */
  246. void rerrfn(REC *rp)
  247. {
  248.     /* if rp is a valid record pointer */
  249.     if (risvalid(rp)) {
  250.  
  251.       /* if reof indicator set */
  252.       if (reof(rp)) {
  253.           fprintf(stderr, "ERROR reading %s -- "
  254.            "tried to read past end of file\n\n", rnames(rp));
  255.  
  256.       /* else rerror indicator set */
  257.       } else {
  258.  
  259.           /* determine cause of error */
  260.           switch (rerror(rp)) {
  261.  
  262.           /* input data errors */
  263.           case R_ERANGE:
  264.           case R_EINVDAT:
  265.           case R_EMISDAT:
  266.           /* output data errors */
  267.           case R_ENOPUT:
  268.  
  269.               /* print location of error */
  270.               fprintf(stderr, "DATA ERROR in FILE %s at LINE %ld,"
  271.                " FIELD %u, COLUMN %u -- %s\n", rnames(rp), rrecno(rp),
  272.                rfldno(rp), max(rcolno(rp)-1, rbegcolno(rp)), rerrstr(rp));
  273.               break;
  274.  
  275.           /* fatal errors (R_EINVMOD, R_EINVAL, R_ENOMEM) */
  276.           default:
  277.             fprintf(errout, "FATAL ERROR reading FILE %s -- %s\n",
  278.              rnames(rp), rerrstr(rp));
  279.             abort();
  280.             break;
  281.           }
  282.       }
  283.  
  284.     /* else invalid record pointer */
  285.     } else {
  286.         switch (errno) {
  287.  
  288.         /* non-fatal errors */
  289.         case EACCES:
  290.         case EMFILE:
  291.           fprintf(errout, "WARNING: %s\n", strerror(errno));
  292.           break;
  293.  
  294.         /* fatal errors (EINVAL, ENOMEM) */
  295.         default:
  296.           fprintf(errout, "FATAL ERROR: %s\n", strerror(errno));
  297.           abort();
  298.           break;
  299.         }
  300.     }
  301. }
  302.  
  303.  
  304. 2.2 Define Callback Warning Function
  305.  
  306. Next define a callback warning function.  You may give the function any name
  307. you want.  In the sample function below, the name rwarnfn is used.  The
  308. function takes one argument, a record pointer (REC *).  It returns nothing
  309. (void).
  310.  
  311. A simple callback warning function rwarnmsg is included in the recio library.  
  312. In the initial prototyping stages of development, you may wish to use this 
  313. function rather than taking the time to develop your own function.  Later 
  314. you can substitute a more robust callback warning function.
  315.  
  316. The recio functions use a callback warning function in order to give the
  317. most flexibility in handling unusual conditions.  For example, the recio
  318. library considers empty data strings to be legal but your application may
  319. want to flag an empty data string as a data error.  You can do that by
  320. checking for R_WEMPSTR warnings in your callback warning function.
  321.  
  322. When your callback warning function is invoked, check rwarning() to
  323. determine the cause of the warning.
  324.  
  325. You can also use your callback warning function to keep track of the number
  326. of warnings, then print a summary of any warnings just before you close a
  327. record stream.  You will find an example in the test program TESTCHP.C.
  328.  
  329. void rwarnfn(REC *rp)
  330. {
  331.   if (risvalid(rp)) {
  332.     switch (rwarning(rp)) {
  333.     case R_WNOREG:   /* atexit() full */
  334.       fprintf (errout, "WARNING %s\n", rwarnstr(rp));
  335.       break;
  336.     case R_WEMPSTR:  /* empty data string */
  337.     case R_WWIDTH:   /* data too wide for columns */
  338.     case R_WTMFMT:   /* time data incomplete */
  339.       fprintf(errout, "WARNING reading %s at record %lu and field %u -- %s\n",
  340.        rnames(rp), rrecno(rp), rfldno(rp), rwarnstr(rp));
  341.       break;
  342.     }
  343.   }
  344. }
  345.  
  346. 2.3 Register Callback Error and Warning Functions
  347.  
  348. Once you have written your callback error and warning functions, you must let
  349. the other recio functions know that they exist.  You use the rseterrfn and
  350. rsetwarnfn functions to individually register your callback functions or 
  351. the rinit to register both functions at once.  You may find use of rinit 
  352. easier to remember.
  353.  
  354.     /* register callback error and warning functions */
  355.     rseterrfn(rerrfn);
  356.     rsetwarnfn(rwarnfn);
  357.  
  358. OR
  359.  
  360.     /* register callback error and warning functions */
  361.     rinit(rerrfn, rwarnfn);
  362.  
  363. OR to use the simple built-in callback error/warning functions
  364.  
  365.     /* register built-in callback error and warning functions */
  366.     rinit(rerrmsg, rwarnmsg);
  367.  
  368.  
  369. OR to use the simple built-in error function without a warning function
  370.  
  371.     /* register callback error function */
  372.     rinit(rerrmsg, NULL);
  373.  
  374.  
  375.  
  376. 3.0 OPEN FILE
  377.  
  378.  
  379. 3.1 Open File and Get Record Pointer
  380.  
  381. Use the ropen function to open the file you want to read, write, or append.
  382. Store the record pointer returned by the ropen function.  Do not open recin,
  383. recout, recerr, or recprn (MSDOS printer).  They are always open, so they do
  384. not need to be opened or closed.
  385.  
  386.     REC *rp = ropen("FILENAME.DAT", "r");
  387.  
  388.  
  389. 3.2 Check Record Pointer
  390.  
  391. Following the ropen function, you need to check to see if the file was
  392. opened correctly.  If ropen returned a NULL pointer, then the file was not
  393. opened.
  394.  
  395. Errors other than ENOENT are reported to your callback error function.
  396. ENOENT is not reported since you may want to use default values if the
  397. data file is not available.
  398.  
  399.     /* if ropen() failed */
  400.     if (!rp) {
  401.         /* if it failed because file does not exist */
  402.         if (errno==ENOENT) {
  403.             /* action to take when file does not exist */
  404.             ...
  405.         }
  406.     /* else ropen() succeeded */
  407.     } else {
  408.         /* set up stream (see sections 3.3 - 3.6) */
  409.         ...
  410.         /* read or write file (see sections 4 and 5) */
  411.         ...
  412.         /* close file (see section 6) */
  413.         rclose(rp);
  414.     }
  415.  
  416.  
  417. 3.3 Set Field and Text Delimiters and Time Format
  418.  
  419. The space character is the default value for both the field and text
  420. delimiters.  The space character matches any white space.  The default 
  421. time format string is "%m/%d/%y".  If you need to something else, use 
  422. the rsetfldch, rsettxtch, and rsettmfmt functions to explicitly set the 
  423. values.  Application maintenance will be easier if you get in the habit 
  424. of setting these values for each record stream.
  425.  
  426.     rsetfldch(rp, ',');         /* set field delimiter character */
  427.     rsettxtch(rp, '"');         /* set text delimiter character */
  428.     rsettmfmt(rp, "%m/%d/%Y");  /* set time format */
  429.  
  430.  
  431. 3.4 Set Field and Record Buffer Sizes
  432.  
  433. Setting the field and record buffer sizes is optional.  Buffers will be
  434. automatically reallocated as necessary.  However if you set the field and
  435. record sizes in advance to the maximum value needed, you can reduce memory
  436. fragmentation.  The field and record buffers are not used for output streams.
  437.  
  438.     rsetfldsiz(rp, 41);  /* set size of field buffer */
  439.     rsetrecsiz(rp, 133); /* set size of record buffer */
  440.  
  441.  
  442. 3.5 Set Context Number
  443.  
  444. If your application opens record streams with more than one data format, you
  445. will want to set a context number.  You use the context number so that your
  446. callback error function can determine (using the rcxtno function) which data
  447. format it is dealing with.  Each context number must be a positive integer;
  448. zero and negative numbers are reserved.  Predefined context symbolic
  449. constants are RECIN, RECOUT, RECERR, and (for MSDOS) RECPRN.
  450.  
  451. #define SOILS_DB      1
  452. #define BUILDINGS_DB  2
  453.  
  454.      rsetcxtno(rp, SOILS_DB); /* set context number */
  455.  
  456.  
  457. 3.6 Set Beginning Column Number
  458.  
  459. The first column number in the record buffer defaults to zero.  If you prefer
  460. column numbering to start at one, use the rsetbegcolno function.  It is mainly
  461. useful if using column delimited data.  If a number takes up the first ten
  462. columns of the record, the column numbering will be 0 to 9 if rsetbegcolno()
  463. is set to 0, or 1 to 10 is rsetbegcolno() is set to 1.
  464.  
  465.      rsetbegcolno(rp, 1); /* first column is column 1 */
  466.  
  467.  
  468.  
  469. 4.0 RECORD FUNCTIONS
  470.  
  471. 4.1 The rgetrec Function
  472.  
  473. If all the records in a data file have the same format, you will want to
  474. loop through all the records until the end of file is reached.  If each
  475. record has a different format, you must call the rgetrec function each
  476. time you want to get the next record.  Calling rgetrec() is optional for
  477. the first record.
  478.  
  479.     /* read all records in file */
  480.     while (rgetrec(rp)) {
  481.         /* Section 5 field functions go here ... */
  482.     }
  483.  
  484.  
  485. 4.2 The rputrec Function
  486.  
  487. After you write all the fields in one record, use the rputrec function
  488. to put the end-of-record newline character to the output and to reset the
  489. internal recio library variables for the next record.
  490.  
  491.     /* write end-of-record */
  492.     rputrec(rp);
  493.  
  494.  
  495. 4.3 The rrecs Macro
  496.  
  497. To get a pointer to the start of the record buffer, use the rrecs macro.
  498.  
  499.     /* echo record contents to stdout */
  500.     printf("%s\n", rrecs(rp));
  501.  
  502.  
  503. 4.4 The rrecno Macro
  504.  
  505. To get the record number, use the rrecno macro.
  506.  
  507.     /* echo record number and record contents to stdout */
  508.     printf("%ld: %s\n", rrecno(rp), rrecs(rp));
  509.  
  510.  
  511. 4.5 The rsetrecstr Function
  512.  
  513. There may be times when you will find it useful to stuff the record buffer
  514. with your own string, then use the field input functions to scan the fields.
  515. Use the rsetrecstr function for this.
  516.  
  517. 4.6 The rresetrec Macro
  518.  
  519. The rresetrec macro resets internals so that you can start reading fields
  520. from the beginning of the record buffer.  This allows you to scan through
  521. the fields in the record buffer multiple times.  For example, you could
  522. first use rskipnfld to determine the number of fields, use rresetrec to
  523. reset, and then read in each field until you have reached the number of
  524. fields.
  525.  
  526.  
  527. 5.0 GET AND PUT FIELD DATA
  528.  
  529. 5.1 Field functions
  530.  
  531. There are 56 functions that can be used to read or write data.  A mnemonic
  532. system makes it easy to construct the name of any function you want.  All you
  533. need to remember is that there are four prefixes, two bodies, and eight
  534. suffixes, and that the rb and rcb prefixes are used only with the i, l, ui,
  535. and ul suffixes.
  536.  
  537. If the prefix contains the letter 'c', it is a column delimited field function.
  538. If the prefix contains the letter 'b', it is a field function that reads or
  539. writes an integral number in a specified base (radix).  The ten suffixes
  540. indicate the ten data types: character, double, float, integer, long,
  541. string, time_t, struct tm, unsigned integer, and unsigned long.
  542.  
  543.     Prefix   Body   Suffix      Prefix   Body   Suffix
  544.     ------   ----   ------      ------   ----   ------
  545.       r       get      c          rb      get      i
  546.       rc      put      d          rcb     put      l
  547.                        f                          ui
  548.                        i                          ul
  549.                        l
  550.                        s
  551.                        t
  552.                       tm
  553.                       ui
  554.                       ul
  555.  
  556.  
  557. 5.2 The rskipfld Macro
  558.  
  559. If your application does not need to read the data in a field, you can skip
  560. over the field by using the rskipfld macro.
  561.  
  562.     /* skip over a field */
  563.     if (rskipfld(rp) != 1) printf("Unable to skip field.\n");
  564.  
  565.  
  566. 5.3 The rskipnfld Function
  567.  
  568. If your application does not need to read the data in several adjacent
  569. fields, you can skip over the fields by using the rskipnfld function.
  570.  
  571.     /* skip over three fields */
  572.     if (rskipnfld(rp, 3) != 3) printf("Unable to skip 3 fields.\n");
  573.  
  574.  
  575. 5.4 The ristxtfld Macro
  576.  
  577. You can use the ristxtfld macro to determine if the current field (the field
  578. most recently input or output) was quoted with the text delimiter character
  579. (provided the text delimiter character is not the space character).  For
  580. example, if you need to write out a string field in the same format in
  581. which was read, you can use this macro to determine if the original field
  582. was quoted.
  583.  
  584.     /* write field in same format as read */
  585.     rgets(recin);
  586.     if (ristxtfld(recin)) rsettxtch(recout, '"');
  587.     else rsettxtch(recout, ' ');
  588.     rputs(recout, rflds(recin));
  589.  
  590.  
  591. 5.5 The reof Macro
  592.  
  593. Use the reof macro to determine when the record stream has reached the
  594. end of file.
  595.  
  596.     /* if error or end of file reached */
  597.     while (rgetrec(rp)) {
  598.     ...
  599.     }
  600.     /* if end of file */
  601.     if (reof(rp)) {
  602.        ...
  603.     /* else error */
  604.     } else {
  605.        ...
  606.     }
  607.  
  608.  
  609. 5.6 The rerror Function
  610.  
  611. Use the rerror function to determine if an error has occurred on a record
  612. stream.  You can access a text string for the error using the rerrstr
  613. function.
  614.  
  615.     if (rerror(rp))
  616.         printf("ERROR reading %s - %s\n",
  617.          rnames(rp), rerrstr(rp));
  618.     rclose(rp);
  619.  
  620. It is a good practice to check for any errors just before closing
  621. a record stream.  If the error indicator is clear, you have additional
  622. confidence that the stream was processed correctly.
  623.  
  624.  
  625. 5.7 The rseterr Function
  626.  
  627. If you write wrapper functions or other functions that interact with
  628. recio functions, your code will need to handle errors.  If can use
  629. the rseterr function to set the error number and to call the record
  630. stream callback error function.
  631.  
  632. /* get integer and validate range */
  633. int rrgeti(REC *rp, int min, int max) {
  634.     int result;
  635.  
  636.     result = rgeti(rp);
  637.     if (result < min || result > max) {
  638.         rseterr(rp, R_ERANGE);
  639.     }
  640.     return result;
  641. }
  642.  
  643.  
  644. 5.8 The rwarning Function
  645.  
  646. Use the rwarning macro to determine if a warning has occurred on a record
  647. stream.
  648.  
  649.     if (rwarning(rp))
  650.         printf("ERROR %s - %s\n", rnames(rp), rwarnstr(rp));
  651.  
  652. It is good practice to check for any warnings just before closing an output
  653. record stream.
  654.  
  655.  
  656. 5.9 The rsetwarn Function
  657.  
  658. If you write wrapper functions or other functions that interact with recio
  659. functions, you may need to provide warnings.  The rsetwarn function sets the
  660. warning number and calls the record stream callback warning function.
  661.  
  662.     rsetwarn(rp, R_WWIDTH);
  663.  
  664.  
  665. 5.10 The rnumfld Function
  666.  
  667. You can determine the number of character delimited fields in the current
  668. record of an input record stream with the rnumfld function.
  669.  
  670.     unsigned numfld;
  671.     
  672.     numfld = rnumfld(recin);
  673.     
  674.  
  675. 5.11 The rgetfldpos and rsetfldpos Functions
  676.  
  677. If you want to mark a specific position within the current record and later 
  678. return to the same position, use the rgetfldpos and rsetfldpos functions. 
  679. If the record number has changed between calls, a R_EINVAL error is generated 
  680. and passed to the callback error function.  The pos argument is passed by 
  681. reference, but a macro takes care of placing the & for you.
  682.  
  683.  
  684.      rpos_t pos;
  685.      
  686.      rgetrec(recin);
  687.      rgetfldpos(recin, pos);   /* get bookmark at start of record */
  688.      rskipnfld(recin, 3);      /* skip three fields */
  689.      rsetfldpos(recin, pos);   /* reset to bookmark position */
  690.  
  691.  
  692.  
  693. 6.0 CLOSE FILE
  694.  
  695. 6.1 Close File
  696.  
  697. When finished reading or writing a data file, close it.  Do not close recin,
  698. recout, recerr, or recprn as they are always open.
  699.  
  700.     /* close record file */
  701.     rclose(rp);
  702.  
  703.  
  704. 6.2 Close All Files
  705.  
  706. Rather than closing record files one at a time, one can close all open
  707. record files at once using the rcloseall function.
  708.  
  709.     /* all done */
  710.     rcloseall();
  711.  
  712.  
  713.  
  714. 7.0 STRING FUNCTIONS
  715.  
  716. The recio library contains a small set of string functions.  Ideally these 
  717. would be part of a larger separate string library, but are included here 
  718. because the recio functions need them.  You may also find them useful for 
  719. your application.
  720.  
  721.  
  722. 7.1 Trim a string
  723.  
  724. The scntrimbegs, scntrimends, and scntrims functions allow you to trim the 
  725. same character from the beginning, end, and both ends of string up to the 
  726. maximum number specified by the last argument.  The sctrimbegs, sctrimends, 
  727. and sctrims functions let you to trim all multiple occurances of the same 
  728. character from the beginning, end, and both ends of a string.  If you 
  729. specify the space character, all whitespace is trimmed.  You can use the 
  730. strimbegs, strimends, and strims macros to trim whitespace from the ends.
  731.  
  732.     char str[]="\t  Hello, World?!!!\n";
  733.     
  734.     strims(str);              /* trim whitespace from both ends of string */
  735.     sctrimends(str, '!');     /* then trim all ! chars from end of string */
  736.     scntrimends(str, '?', 1); /* finally trim one ? char from end of string */
  737.  
  738.  
  739. 7.2 Dynamically copy and concatenate a string
  740.  
  741. The scpys and scats macros are used to dynamically copy and concatenate a 
  742. string.  To use these macros you must adhere to these points:
  743.  
  744.      1. Initialize pointers of destination strings to NULL.
  745.      2. These macros pass the destination string by reference (and place
  746.         the & for you).
  747.      3. A pointer to the destination string is returned.
  748.      4. Free any dynamically allocated strings when finished with them.
  749.      
  750.      char *dst=NULL;
  751.      char src1[]="Hello ";
  752.      char src2[]="world";
  753.      
  754.      scpys(dst, src1);
  755.      puts(scats(dst, src2));  /* "Hello world" */
  756.  
  757.      /* however the following will not work here as in C++ where you can 
  758.       * declare a function to pass an argument by reference, i.e. 
  759.       * char *scats(char *&dst, const char *src)
  760.       *
  761.       * puts(scats(scpys(dst, src1), src2));  /* C++ */
  762.       */
  763.      
  764.      free(dst);
  765.  
  766.  
  767.  
  768. 8.0 TIME CONVERSION FUNCTIONS
  769.  
  770. The recio library contains a few time conversion functions.
  771.  
  772.  
  773. 8.1 Convert from string to time_t or struct tm types.
  774.  
  775. You can convert a string to either the time_t or struct tm time types using 
  776. the sftotime or sftotm functions.  These functions complement the ANSI 
  777. X3.159-1989 strftime function.  However sftotime and sftotm use a subset 
  778. of the specifiers found with strftime.  The recio library uses the sftotime 
  779. and sftotm functions to get time from the input stream, but uses the strftime 
  780. function to put time to the output stream.  As a consequence, you can use the 
  781. complete set of strftime specifiers for any output you do not later have to 
  782. read back into a program.  If there is an error, the struct tm type will 
  783. contain the time "01/01/70 00:00:00" and the time_t type will contain the 
  784. value (time_t)-1, i.e. -1 cast to a type_t type.
  785.  
  786.  
  787.      time_t time;
  788.      struct tm t;
  789.      
  790.      time = sftotime("11/24/94", "%m/%d/%y");
  791.      t = sftotm("11/24/94", "%m/%d/%y");
  792.  
  793.  
  794. 8.2 Convert between time_t and struct tm types.
  795.  
  796. You can convert between the time_t and struct tm types using the tmtotime and 
  797. timetotm functions.  The functions are similar to the mktime and localtime 
  798. functions in ANSI X3.159-1989, but the arguments don't use pointers and they 
  799. may be easier to remember.  If there is an error, the struct tm type will 
  800. contain the time "01/01/70 00:00:00" and the time_t type will contain the 
  801. value (time_t)-1, i.e. -1 cast to a type_t type.
  802.  
  803.      time_t time;
  804.      struct tm t;
  805.      
  806.      time = sftotime("12/25/94", "%m/%d/%y");
  807.      t = timetotm(time);
  808.  
  809.  
  810.  
  811. 9.0 REFERENCES
  812.  
  813. 1. ANSI X3.159-1989.  American National Standard for Information Systems -
  814.    Programming Language - C.  American National Standards Institute,
  815.    11 West 42nd Street, New York, NY 10036, 1990.
  816.  
  817.  
  818.  
  819. 10.0 INDEX
  820.  
  821. errno macro ............ 2.0, 2.1, 3.2
  822. rbegcolno macro ........ 2.1
  823. rclearerr function ..... 2.1
  824. rclose function ........ 6.1
  825. rcloseall function ..... 6.2
  826. rcolno macro ........... 2.1
  827. rcxtno macro ........... 3.5
  828. recin expression ....... 3.1, 6.1
  829. reof macro ............. 2.1, 5.5
  830. rerror function ........ 2.1, 5.6
  831. rerrmsg function ....... 2.1, 2.3
  832. rerrstr function ....... 2.1, 5.6
  833. rfix functions ......... 2.1
  834. rflds macro ............ 5.4
  835. rfldno macro ........... 2.1
  836. rbget functions ........ 5.1
  837. rbput functions ........ 5.1
  838. rcbget functions ....... 5.1
  839. rcbput functions ....... 5.1
  840. rcget functions ........ 5.1
  841. rcput functions ........ 5.1
  842. rget functions ......... 5.1
  843. rgetfldpos function .... 5.11
  844. rgetrec function ....... 4.1
  845. rinit function ......... 2.3
  846. ristxtfld macro ........ 5.4
  847. risvalid function ...... 2.1
  848. rnames macro ........... 2.1
  849. rnumfld function ....... 5.10
  850. ropen function  ........ 3.1
  851. rput functions ......... 5.1
  852. rrecs macro ............ 2.1, 4.3
  853. rrecno macro ........... 2.1, 4.4
  854. rresetrec macro ........ 4.6
  855. rsetbegcolno function .. 3.6
  856. rsetcxtno function ..... 3.5
  857. rseterr function ....... 5.7
  858. rseterrfn function ..... 2.3
  859. rsetfldch function ..... 3.3
  860. rsetfldpos function .... 5.11
  861. rsetfldsiz function .... 3.4
  862. rsetfldstr function .... 2.1
  863. rsetrecsiz function .... 3.4
  864. rsetrecstr function .... 4.5
  865. rsettmfmt function ..... 3.3
  866. rsettxtch function ..... 3.3, 5.4
  867. rsetwarn function ...... 5.9
  868. rsetwarnfn function .... 2.3
  869. rskipfld macro  ........ 5.2
  870. rskipnfld function ..... 5.3
  871. rwarning function ...... 2.2, 5.8
  872. rwarnmsg function ...... 2.2, 2.3
  873. rwarnstr function ...... 2.2, 5.7
  874. scats macro ............ 7.2
  875. scpys macro ............ 7.2
  876. sctrimbegs function .... 7.1
  877. sctrimends function .... 7.1
  878. sctrims function ....... 7.1
  879. sftotime function ...... 8.1
  880. sftotm function ........ 8.1
  881. strimbegs macro ........ 7.1
  882. strimends macro ........ 7.1
  883. strims macro ........... 7.1
  884. timetotm function ...... 8.2
  885. tmtotime function ...... 8.2
  886.